/*
 * Login.java
 *
 * Created on March 28, 2007, 4:20 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.actions;

import Data.CustomerBO;
import Data.CustomerDAO;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 *
 * @author Admin
 */
public class Login implements edu.byu.isys413.web.Action {
    
    /** Creates a new instance of Login */
    public Login() {
    }
    
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        
        if(request.getParameter("email").equals("")){
            request.setAttribute("message","Please enter your username and password");
            return "login.jsp";
        }
        
        List<CustomerBO> customerList = null;
        try {
            String email = request.getParameter("email");
            customerList = CustomerDAO.getInstance().getByEmail(email);
        }catch (Exception ex) {
            request.setAttribute("message","Login Credentials Invalid. PLEASE re-enter your email and password");
            ex.printStackTrace();
            return "login.jsp";
        }
        
        for (int i = 0; i < customerList.size(); i++) {
            if(request.getParameter("password").equals(customerList.get(i).getMember().getPassword())){
                session.setAttribute("customer",customerList.get(i));
                return "index.jsp";
            }
        }
        
        
        request.setAttribute("message","Your username does not match your password");
        return "login.jsp";
        
    }//process
}
